﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using POIParser;

namespace RefineNerTrainingCorpus
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 4)
            {
                Console.WriteLine("RefineNerTrainingCorpus [Location lexical file name] [CRF model file name] [Input NER training corpus] [Refined NER training corpus]");
                return;
            }

            POIParser.POIParser parser = new POIParser.POIParser();
            parser.Initialize(args[0], args[1]);

            StreamReader sr = new StreamReader(args[2]);
            StreamWriter sw = new StreamWriter(args[3]);

            while (sr.EndOfStream == false)
            {

                //Read a record line
                string strLine = sr.ReadLine();

                string[] items = strLine.Split();
                string strOutput = "";

                foreach (string item in items)
                {
                    int pos = item.IndexOf('[');
                    string strTerm;
                    string strTag;

                    strTag = item.Substring(pos + 1, item.Length - pos - 1 - 1);
                    strTerm = item.Substring(0, pos);

                    if (strTag == "ORG" || strTag == "SCENE" || strTag == "POI")
                    {
                        List<Token> tokenList;
                        tokenList = parser.Parse(strTerm);

                        for (int i = 0; i < tokenList.Count; i++)
                        {
                            strOutput = strOutput + tokenList[i].strTerm + "[" + tokenList[i].strTag + "] ";
                        }
                    }
                    else
                    {
                        strOutput = strOutput + strTerm + "[" + strTag + "] ";
                    }
                }
                sw.WriteLine(strOutput.Trim());
            }
            sr.Close();
            sw.Close();

        }
    }
}
